package com.qf.java2201.dao.impl;

import com.qf.java2201.dao.OrderDao;
import com.qf.java2201.pojo.HOrder;
import com.qf.java2201.utils.DbUtils1;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class OrderDaoImpl implements OrderDao{
    QueryRunner queryRunner = new QueryRunner(DbUtils1.getDs());
    @Override
    public int getPayOrderCount(String odate, String sjdid, String edid, int yyztid) {
        //查询当前已付款的预约单个数

        String sql = "select count(*) from h_order where edid = ? and odate = ? and sjdid = ? and yyztid = ? ";

        Long query = null;
        try {
            query = queryRunner.query(sql, new ScalarHandler<Long>(), edid, odate, sjdid, yyztid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query.intValue();
    }

    @Override
    public int addOrder(HOrder hOrder) {
        String sql = "insert into h_order values (null,?,?,?,?,?,?,?,now())";


        BigInteger oid = null;
        try {
            int update = queryRunner.update(sql, hOrder.getEdid(), hOrder.getUdid(), hOrder.getSjdid(), hOrder.getYyztid(),
                    hOrder.getSickness(), hOrder.getGhf(), hOrder.getOdate());

            //查询刚才插入的数据的主键值
            String sql2 = "select LAST_INSERT_ID()";
            oid = queryRunner.query(sql2, new ScalarHandler<BigInteger>());   //  结果集中 是一个数  使用ScalarHandler 来进行转换
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return oid.intValue();
    }

    @Override
    public int updateOrderStatus(String oid, Integer yyztid) {
        String sql = "update h_order set yyztid = ? where oid = ?";

        int update = 0;
        try {
            update = queryRunner.update(sql, yyztid, oid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update;
    }

    @Override
    public List<HOrder> findOrderByUdid(Integer did) {
        String sql = "SELECT e.drealname as ename, o.odate, t1.tname AS sjd, o.ghf, t2.tname AS yyzt, o.yyztid, o.oid FROM h_order o JOIN h_userinfo u ON o.udid = u.did JOIN h_type t1 ON o.sjdid = t1.tid JOIN h_type t2 ON o.yyztid = t2.tid  JOIN h_userinfo e ON o.edid = e.did WHERE u.did =  ?";

        List<HOrder> query = null;
        try {
            query = queryRunner.query(sql, new BeanListHandler<>(HOrder.class), did);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    @Override
    public int countDayQxCount(String oid, Integer did, Date odate) {
        String sql = "select count(*) from h_order where udid = ? and yyztid = 23 and odate = ?";

        Long query = null;
        try {
            query = queryRunner.query(sql, new ScalarHandler<Long>(), did, odate);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query.intValue();
    }

    @Override
    public int countWeekQxCount(String oid, Integer did, Date odate) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(odate);

        //把日历 翻到 这个日期所在周的周一

        instance.set(Calendar.DATE, instance.get(Calendar.DATE) - (instance.get(Calendar.DAY_OF_WEEK) - 2));

        //周一的日期

        Date zy = instance.getTime();

        //把日历 翻到 这个日期所在的周的周五
        instance.set(Calendar.DATE,instance.get(Calendar.DATE)-(instance.get(Calendar.DAY_OF_WEEK) - 2) + 4);

        //周五的日期
        Date zw = instance.getTime();
        String sql = "select count(*) from h_order where udid = ? and yyztid = 23 and odate <= ? and odate >= ?";

        Long query = null;
        try {
            query = queryRunner.query(sql, new ScalarHandler<Long>(), did, zw, zy);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query.intValue();
    }
}
